﻿/**
说明：系统框架通用js，减少js编写
Dai 2020.11.14
**/

/**
将form表单转成json对象
formID:form表单Id
**/
function FormToJson(formID) {
    var unindexed_array = $('#' + formID).serializeArray();
    var indexed_array = {};
    $.map(unindexed_array, function (n, i) {
        indexed_array[n['name']] = n['value'];
    });
    return indexed_array;
}
/**
通用页面打开弹出方法(不加载数据)
**/
function OpenPage(title, pageUrl) {
    if (title == undefined || title == "") {
        title = "新建窗口";
    }
    var w = window.screen.width;
    var h = window.screen.height;

    var url = pageUrl;
    layer.open({
        type: 2,
        title: title,
        skin: 'layui-layer-rim', //加上边框
        area: ['90%', '85%'],
        content: url,
        offset: "auto"
    });
}
/**
弹出页面关闭方法
**/
function closeWin() {
    var index = parent.layer.getFrameIndex(window.name);
    //关闭当前frame
    parent.layer.close(index);
}
/**
获取Url地址中的参数
**/
function getUrlVariable(variable) {
    var query = decodeURIComponent(window.location.search.substring(1));
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}
/**
单点登录条码客户端方法
**/
function SSO(type, dataId) {
    $.ajax({
        url: "../System/RunExe",
        data: { dataType: type, dataId: dataId },
        dataType: "json",
        type: "POST",
        success: function (r) {
            if (r.Result) {
                window.location.href = "printBarTools://" + r.Data;
            } else {
                layer.msg(r.Message);
            }
        }

    });
}
/**
获取日期
**/
function getDate(n) {
    var date = new Date();
    var year, month, day;
    date.setDate(date.getDate() + n);
    year = date.getFullYear();
    month = date.getMonth() + 1;
    day = date.getDate();
    s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
    return s;
}
/** 
div高度自适应屏幕
**/
function autoHeight(divId) {
    var height = $(window).height();

    $("#" + divId).height(height - 10);

}
/**
验证登录信息是否丢失
**/
function checkOnline() {
    $.ajax({
        url: "../System/isCheckOnline",
        dataType: "json",
        type: "POST",
        success: function (rsl) {
            if (!rsl.Result) {
                window.location.href = "../System/Login";
            }
            else {
                InService();
            }
        }
    });
    //InService();
}

/**
验证系统权限
**/
function InService() {
    $.ajax({
        url: "../System/CheckInService",
        dataType: "json",
        type: "POST",
        success: function (rsl) {
            if (!rsl.Result) {
                layer.msg(rsl.Message);
                window.location.href = "../System/Login";
            }
        }
    });
}
/**
  信息列表页打开页面方法(暂时不用)
   url：目标页地址
   title：页面窗体标题
   width：窗体宽度
   height：窗体高度
   注意：该方法只应用于列表页，因为在关闭子页面时需要回调数据加载方法
   **/
function InfoOpenPage(url, title, width, height) {
    layer.open({
        type: 2,
        maxmin: false,
        fix: false,
        title: title,
        area: [width, height],
        closeBtn: 1,
        shadeClose: true,
        content: url,
        //关闭弹出框的回调函数，不管弹出框是手动关闭还是通过脚本进行关闭，都执行
        end: function () {
            loadData();
        }
    });
}
/**
新页面加载方法
webUrl：目标页面地址
title：打开窗体标题
width:宽度
height:高度
tableId：LayUI数据表ID
curr：当前数据表页码
dUrl:数据源地址
**/
function NewOpenPage(webUrl, title, width, height,tableId, curr, dUrl) {
    var table = layui.table; //LayUI数据表对象
    layer.open({
        type: 2,
        maxmin: false,
        fix: false,
        title: title,
        area: [width, height],
        closeBtn: 1,
        shadeClose: true,
        content: webUrl,
        //关闭弹出框的回调函数，不管弹出框是手动关闭还是通过脚本进行关闭，都执行
        end: function () {
            if (tableId != null && tableId != "") {
                table.reload(tableId, {
                    url: dUrl
                           , page: {
                               curr: curr,//currPage是全局变量，后面会给出它在哪里定义以及赋值
                    }
                });
            }
       
        }
    });
}
/**
重载数据表格
tableId:数据表id
dataUrl：数据源地址
currPage：页码
**/
function reLoadTable(tableId, dataUrl, currPage) {
    var table = layui.table; //LayUI数据表对象
    table.reload(tableId, {
        url: dataUrl
    , page: {
        curr: currPage,//currPage是全局变量，后面会给出它在哪里定义以及赋值
    }
    });
}
/**
表格背景颜色
DivId：表格外层ID
RowIndex：行索引
Color：颜色
**/
function TableRowColor(DivId, RowIndex, Color) {
    try {
        var div = document.getElementById(DivId);
        if (div != null) //找到对象了
        {
            var table_main = div.getElementsByClassName("layui-table-main");   //通过class获取table_main
            if (table_main != null && table_main.length > 0) {
                var table = table_main[0].getElementsByClassName('layui-table');   //通过class获取table
                if (table != null && table.length > 0) {
                    var trs = table[0].querySelectorAll("tr");
                    if (trs != null && trs.length > 0) {
                        trs[RowIndex].style.backgroundColor = Color;//背景颜色
                    }
                }
            }

        }
    }
    catch (e) {
        console.log(e.message);
    }
}
/**
表格背景颜色
DivId：表格外层ID
RowIndex：行索引
tdIndex:单元格索引
Color：颜色
**/
function TableTdColor(DivId, RowIndex, tdIndex, Color) {
    try {
        var div = document.getElementById(DivId);

        if (div != null) //找到对象了
        {
            var table_main = div.getElementsByClassName("layui-table-main");   //通过class获取table_main
            if (table_main != null && table_main.length > 0) {
                var table = table_main[0].getElementsByClassName('layui-table');   //通过class获取table
                if (table != null && table.length > 0) {
                    var trs = table[0].querySelectorAll("tr");
                    let tds = trs[RowIndex].querySelectorAll("td");
                    tds[tdIndex].style.backgroundColor = Color;

                }
            }

        }
    }
    catch (e) {
        console.log(e.message);
    }
}
/**
checkbox设置默认选中值
**/
function SetCheckBoxValue(kjName, values) {
    var str = values.split(',');
    $("input[name='" + kjName + "']").each(function () {
        for (var i = 0; i < str.length; i++) {
            if ($(this).attr('value').toLowerCase() == str[i].toString().toLowerCase()) {
                $(this).attr("checked", 'true');
                break;
            }
        }

    });
}
/**
  时间戳获取方法
  **/
var dateTimeStr = function () {
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var hour = date.getHours().toString();
    var minutes = date.getMinutes().toString();
    var seconds = date.getSeconds().toString();
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    return y + (m < 10 ? ('0' + m) : m) + (d < 10 ? ('0' + d) : d) + hour + minutes + seconds;
}
var tCloumns = "";
/**
获取表格列信息
tCode:表格编码
**/
function getTableCloums(tCode) {
    $.ajax({
        url: "../System/GetTableCloums",
        data:{"tCode":tCode},
        dataType: "json",
        type: "POST",
        async: false,
        success: function (rsl) {
            if (rsl.Result) {
                tCloumns = rsl.Data[0]["columns"].toString();
            } else {
                layer.msg(rsl.Message); return;
            }
        }
    });
}
/**
新建Tab页签打开界面
**/
function NewTabWeb(title, url, is_refresh) {
    var element = parent.layui.element;
    var id = md5(url);//md5每个url
    //防止重复打开
    for (var i = 0; i < parent.$('.x-iframe').length; i++) {
        if (parent.$('.x-iframe').eq(i).attr('tab-id') == id) {
            element.tabChange('xbs_tab', id);
            if (is_refresh)
                parent.$('.x-iframe').eq(i).attr("src", parent.$('.x-iframe').eq(i).attr('src'));
            return;
        }
    };
    element.tabAdd('xbs_tab', {
        title: title
        , content: '<iframe tab-id="' + id + '" frameborder="0" src="' + url + '" scrolling="yes" class="x-iframe"></iframe>'
        , id: id
    });
    element.tabChange('xbs_tab', id);
}
/**
页面按钮显示设置(操作权限)
menuCode:页面编码
**/
var userPower = "";//人员权限
var webPower = "";//
function WebPowersShow() {
    var mCode = getUrlVariable("menuCode");
    $.ajax({
        url: "../System/GetWebPowers",
        data: { menuCode: mCode },
        dataType: "json",
        type: "POST",
        async: false,//同步
        success: function (rsl) {
            if (rsl.Result) {
                userPower = rsl.Data["userPower"];
                webPower = rsl.Data["webPower"];
                HideButton();
            }

        }
    })
}
/**
设置按钮隐藏方法
**/
function HideButton() {
    var uArr = userPower.split(",");
    var wArr = webPower.split(",");
    for (var i = 0; i < wArr.length; i++) {
        var isHide = true;
        for (var x = 0; x < uArr.length; x++) {
            if (wArr[i] == uArr[x]) {
                isHide = false;
                break;
            }
        }
        if (isHide) {
            $("#btn-" + wArr[i]).hide();
        }
    }
    $("button").each(function () {
        var btnId = $(this).attr("id");
        if (btnId != undefined) {
            var arr = btnId.split("-");
            var hide = true;
            if (arr.length > 1) {
                for (var i = 0; i < wArr.length; i++) {
                    if (wArr[i] == arr[1]) {
                        hide = false;
                        break;
                    }
                }
            }
            if (hide) {
                $("#" + btnId).hide();
            }
        }
    })
}
/**
表格权限隐藏设置
**/
function TablePowerHide() {
    var uArr = userPower.split(",");
    var wArr = webPower.split(",");
    for (var i = 0; i < wArr.length; i++) {
        var isHide = true;
        for (var x = 0; x < uArr.length; x++) {
            if (wArr[i] == uArr[x]) {
                isHide = false;
                break;
            }
        }
        if (isHide) {
            $(".layui-table-cell #" + wArr[i]).css("display", 'none');
        }
    }
//隐藏无关按钮
    $("div .layui-table-body").find("tr").each(function () {
        var a = $(this).find("td");
        if (a != undefined && a != null && a.length > 0) {
            a.each(function () {
                var field = $(this).attr("data-field");
                if (field != undefined && field != null && field == "right") {
                    var div = a.find("div");
                    if (div != undefined && div != null) {
                        div.find("a").each(function () {
                            var btnId = $(this).attr("id");
                            var isHide = true;
                            for (var i = 0; i < wArr.length; i++) {
                                if (btnId == wArr[i]) {
                                    isHide = false; break;
                                }
                            }
                            if (isHide) {
                                $(".layui-table-cell #" + btnId).css("display", 'none');
                            }
                        });
                    }
                }
            });
        }
    });
}